package com.qbw.core.base;

import android.app.Activity;

import androidx.collection.ArrayMap;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import android.os.Bundle;
import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lib.util.ResUtil;
import com.qbw.log.XLog;

import java.util.Map;
import java.util.Set;

/**
 * Created by Bond on 2017/01/16 16:09
 * you can contact me at qbaowei@qq.com
 */


public abstract class BaseFragment<B extends ViewDataBinding> extends Fragment {

    private final String TAG = "[life]";

    protected B mBinding;

    private Map<Long, BaseFragment> mFragmentMap = new ArrayMap<>();

    private String getLogTag() {
        return TAG + getClass().getSimpleName();
    }

    protected abstract @LayoutRes
    int getLayoutResId();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        XLog.v(getClass().getSimpleName());
        mBinding = DataBindingUtil.inflate(inflater, getLayoutResId(), container, false);
        onInit();
        return mBinding.getRoot();
    }

    protected void onInit() {

    }

    @Override
    public void onResume() {
        super.onResume();
        XLog.v(getLogTag());
        //MobclickAgent.onPageStart(getClass().getSimpleName());
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        XLog.v("%s,hidden=%b", getLogTag(), hidden);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        XLog.v("%s,isVisibleToUser=%b", getLogTag(), isVisibleToUser);
    }

    @Override
    public void onPause() {
        super.onPause();
        XLog.v(getLogTag());
        //MobclickAgent.onPageEnd(getClass().getSimpleName());
    }

    @Override
    public void onDestroyView() {
        removeAllFragments();
        super.onDestroyView();
        XLog.v(getLogTag());
    }

    public void switchFragment(long id) {
        if (mFragmentMap.isEmpty()) {
            return;
        }
        BaseFragment fragment = mFragmentMap.get(id);
        if (null != fragment) {
            hideFragmentExcept(id);
            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            if (!fragment.isAdded()) {
                XLog.d("addCommonParams fragment %s", fragment.getClass().getSimpleName());
                transaction.add(getContainerViewId(), fragment);
            }
            XLog.d("show fragment %s", fragment.getClass().getSimpleName());
            transaction.show(fragment);
            transaction.commitAllowingStateLoss();
        } else {
            XLog.e("no fragment for id %d");
        }
    }

    public void addFragment(long id, BaseFragment fragment) {
        if (mFragmentMap.containsKey(id) && mFragmentMap.get(id) != null) {
            XLog.w("碎片%d已经存在", id);
            return;
        }
        mFragmentMap.put(id, fragment);
    }

    public boolean isFragmentContains(long id) {
        return !mFragmentMap.isEmpty() && mFragmentMap.containsKey(id);
    }

    private void hideFragmentExcept(long id) {
        if (mFragmentMap.isEmpty()) {
            return;
        }
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        Set<Map.Entry<Long, BaseFragment>> fragments = mFragmentMap.entrySet();
        for (Map.Entry<Long, BaseFragment> fragment : fragments) {
            if (fragment.getKey() != id) {
                BaseFragment baseFragment = fragment.getValue();
                if (baseFragment.isVisible()) {
                    XLog.d("hide fragment %s", baseFragment.getClass().getSimpleName());
                    transaction.hide(baseFragment);
                }
            }
        }
        transaction.commitAllowingStateLoss();
    }

    private void removeAllFragments() {
        if (mFragmentMap.isEmpty()) {
            return;
        }
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        for (Fragment fragment : mFragmentMap.values()) {
            transaction.remove(fragment);
        }
        transaction.commitAllowingStateLoss();
        mFragmentMap.clear();
        XLog.d("%s:remove all fragments finished.", getLogTag());
    }

    protected int getContainerViewId() {
        return 0;
    }

    public void showLoadingDialog(@StringRes int titleRes) {
        showLoadingDialog(ResUtil.getString(titleRes));
    }

    public void showLoadingDialog(String title) {
        Activity activity = getActivity();
        if(activity instanceof BaseActivity) {
            ((BaseActivity) activity).showLoadingDialog(title);
        }
    }

    public void closeLoadingDialog() {
        Activity activity = getActivity();
        if(activity instanceof BaseActivity) {
            ((BaseActivity) activity).closeLoadingDialog();
        }
    }
}
